using System;
using System.Collections.Generic;
using System.Linq;

namespace Framework.DataObjects
{
    public static class KeyValueExtensions
    {
        /// <summary>
        /// Set the named parameter with a value.
        /// </summary>
        /// <param name="keyValues">The key/Values.</param>
        /// <param name="key">The name.</param>
        /// <param name="value">The value.</param>
        public static void Set(this List<KeyValuePair<string, string>> keyValues, string key, string value)
        {
            Set(keyValues, -1, key, value);
        }

        /// <summary>
        /// Sets the specified key/Values.
        /// </summary>
        /// <param name="keyValues">The key/Values.</param>
        /// <param name="pair">The pair.</param>
        public static void Set(this List<KeyValuePair<string, string>> keyValues, KeyValuePair<string, string> pair)
        {
            Set(keyValues, -1, pair.Key, pair.Value);
        }

        /// <summary>
        /// Set the named key/Values with a value.
        /// </summary>
        /// <param name="keyValues">The key/Values</param>
        /// <param name="insertIndex">Index of the insert.</param>
        /// <param name="key">The name.</param>
        /// <param name="value">The value.</param>
        public static void Set(this List<KeyValuePair<string, string>> keyValues, int insertIndex, string key, string value)
        {
            var paramIndex = GetIndex(keyValues, key);

            var storeValue = Is.IsNull(value) ? string.Empty : value;

            if (paramIndex == -1)
            {
                var param = new KeyValuePair<string, string>(key, storeValue);

                if (insertIndex == -1 || insertIndex > keyValues.Count - 1)
                {
                    keyValues.Add(param);
                }
                else
                {
                    keyValues.Insert(insertIndex, param);
                }
            }
            else
            {
                keyValues[paramIndex] = new KeyValuePair<string, string>(key, value);
            }
        }

        /// <summary>
        /// Get the key/Values as a value, defaults to String.Empty if not found.
        /// </summary>
        /// <param name="keyValues"></param>
        /// <param name="key">The name.</param>
        /// <returns></returns>
        public static string GetValue(this IEnumerable<KeyValuePair<string, string>> keyValues, string key)
        {
            return GetValue(keyValues, key, String.Empty);
        }

        /// <summary>
        /// Get the parameter as a value, uses a default value if not found.
        /// </summary>
        /// <param name="keyValues"></param>
        /// <param name="key">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static string GetValue(this IEnumerable<KeyValuePair<string, string>> keyValues, string key, string defaultValue)
        {
            var param = Get(keyValues, key);

            return param.Equals(default(KeyValuePair<string,string>)) ? defaultValue : StrUtil.DefaultValue(param.Value, defaultValue);
        }

        /// <summary>
        /// Gets the parameter.
        /// </summary>
        /// <param name="keyValues">The key values.</param>
        /// <param name="key">The name.</param>
        /// <returns></returns>
        public static KeyValuePair<string, string> Get(this IEnumerable<KeyValuePair<string, string>> keyValues, string key)
        {
            return keyValues.FirstOrDefault(parameter => Is.EqualString(parameter.Key, key));
        }

        /// <summary>
        /// Gets the parameter index.
        /// </summary>
        /// <param name="keyValues"></param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static int GetIndex(this List<KeyValuePair<string, string>> keyValues, string name)
        {
            for (var index = 0; index < keyValues.Count; index++)
            {
                var parameter = keyValues[index];

                if (Is.EqualString(parameter.Key, name))
                {
                    return index;
                }
            }
            return -1;
        }

        /// <summary>
        /// Converst the list of Name/Values to dictionary of strings
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        public static IDictionary<string, string> ToDictionary(this List<KeyValuePair<string, string>> parameters)
        {
            return parameters.ToDictionary(parameter => parameter.Key, parameter => parameter.Value);
        }
    }
}